package structure.proxy.dynamicproxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
    /**
     * 被代理接口
     */
    private Object target;



    public ProxyInvocationHandler(Object target) {
        this.target = target;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // todo 这部分可以处理代理对象的逻辑
        System.out.println("代理对象调用之前");
        Object res = method.invoke(target, args);
        System.out.println("代理对象调用之后");
        return res;
    }

}
